package de.hijacksoft.isle;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import de.hijacksoft.isle.adapters.DBAdapter;
import de.hijacksoft.isle.adapters.StatusListener;
import de.hijacksoft.isle.adapters.SyncAdapter;
import de.hijacksoft.isle.classes.Item;
import java.util.ArrayList;

public class SyncActivity extends Activity implements StatusListener {
    
    private static final String TAG = "SyncActivity";
    
    private SyncAdapter syncAdapter;
    private DBAdapter dbAdapter;
    
    private Button backup, restore;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
        
        createUI();
        initSyncAdapter();
        initDB();

    }
            
    private void initSyncAdapter(){
        syncAdapter = SyncAdapter.getInstance();
        syncAdapter.addStatusListener(this);
    }
    
    private void initDB() {
        dbAdapter = new DBAdapter(this);
        dbAdapter.open();
    }
    private void createUI(){
        
        setContentView(R.layout.syncactivity);
        
        backup = (Button) findViewById(R.id.syncactivity_btn_backup);
        restore = (Button) findViewById(R.id.syncactivity_btn_restore);
        
        backup.setOnClickListener(new OnClickListener(){

            public void onClick(View arg0) {
                syncAdapter.writeDatabase();
            }
            
        });
        
        restore.setOnClickListener(new OnClickListener(){

            public void onClick(View arg0) {
                syncAdapter.readDatabase();
            }
            
        });

    }

        @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:{
                break;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        syncAdapter.close();
        dbAdapter.close();
    }

    public void onStatusChange(int status, SyncAdapter adapter) {
        
        switch(status){
            case SyncAdapter.STATUS_READ_FINISHED:{ 
                ArrayList<Item> itemDB = adapter.getDatabase();
                dbAdapter.restoreItemsTable(itemDB);
                Toast.makeText(this, R.string.sync_restorefinished, Toast.LENGTH_LONG).show();
                break;
            }
            case SyncAdapter.STATUS_WRITE_FINISHED:{ 
                Toast.makeText(this, R.string.sync_backupfinished, Toast.LENGTH_LONG).show();
                break;
            }
            case SyncAdapter.STATUS_DISCONNECTED: finish(); break;
        }
        
    }

}